package com.tools.web.filter.gateway;

import com.tools.common.object.Note;
import com.tools.common.thread.TimePair;
import com.tools.common.thread.Timeunit;

import java.util.Objects;

/**
 * 启动拉黑模式的配置项包装类
 * */
@Note("启动拉黑模式的配置项包装类")
public class AddBlacker {

    @Note("【条件1】：不能为空。缓存的 key 的前缀。")
    private String saveKeyPrefix;

    @Note("【条件2】：不为 null 且时间值大于 0。在该时间范围内")
    private TimePair thresholdTime;

    @Note("【条件3】：不能为 null。访问次数大于等于该阈值就拉黑")
    private int threshold;

    /* ********************************************************************************************
     *
     *         构造器
     *
     * *********************************************************************************************
     * */

    public AddBlacker(TimePair thresholdTime, int threshold) {
        this(null, thresholdTime, threshold);
    }

    public AddBlacker(long thresholdTime, Timeunit thresholdTimeunit, int threshold) {
        this(null, thresholdTime, thresholdTimeunit, threshold);
    }

    public AddBlacker(String saveKeyPrefix, long thresholdTime, Timeunit thresholdTimeunit, int threshold) {
        this.setSaveKeyPrefix(saveKeyPrefix);
        this.setThresholdTime(thresholdTime, thresholdTimeunit);
        this.setThreshold(threshold);
    }

    public AddBlacker(String saveKeyPrefix, TimePair thresholdTime, int threshold) {
        this.setSaveKeyPrefix(saveKeyPrefix);
        this.setThresholdTime(thresholdTime);
        this.setThreshold(threshold);
    }

    public static AddBlacker of(TimePair thresholdTime, int threshold) {
        return new AddBlacker(thresholdTime, threshold);
    }

    public static AddBlacker of(long thresholdTime, Timeunit thresholdTimeunit, int threshold) {
        return new AddBlacker(thresholdTime, thresholdTimeunit, threshold);
    }

    public static AddBlacker of(String saveKeyPrefix, long thresholdTime, Timeunit thresholdTimeunit, int threshold) {
        return new AddBlacker(saveKeyPrefix, thresholdTime, thresholdTimeunit, threshold);
    }

    public static AddBlacker of(String saveKeyPrefix, TimePair thresholdTime, int threshold) {
        return new AddBlacker(saveKeyPrefix, thresholdTime, threshold);
    }

    /* ********************************************************************************************
     *
     *         Getter / Setter
     *
     * *********************************************************************************************
     * */

    public String getSaveKeyPrefix() {
        return saveKeyPrefix;
    }

    public void setSaveKeyPrefix(String saveKeyPrefix) {
        this.saveKeyPrefix = saveKeyPrefix;
    }

    public TimePair getThresholdTime() {
        return thresholdTime;
    }

    public void setThresholdTime(TimePair thresholdTime) {
        if(thresholdTime != null && thresholdTime.isEffectiveTime()) {
            this.thresholdTime = thresholdTime;
        }
    }

    public void setThresholdTime(long thresholdTime, Timeunit thresholdTimeunit) {
        if(thresholdTimeunit != null && thresholdTime > 0) {
            this.thresholdTime = new TimePair(thresholdTime, thresholdTimeunit);
        }
    }

    public int getThreshold() {
        return threshold;
    }

    public void setThreshold(int threshold) {
        this.threshold = threshold;
    }

    @Override
    public String toString() {
        return "AddBlacker{" +
                "saveKeyPrefix=" + saveKeyPrefix +
                ", thresholdTime=" + thresholdTime +
                ", threshold=" + threshold +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AddBlacker that = (AddBlacker) o;
        return Objects.equals(thresholdTime, that.thresholdTime) && threshold == that.threshold && Objects.equals(saveKeyPrefix, that.saveKeyPrefix);
    }

    @Override
    public int hashCode() {
        return Objects.hash(saveKeyPrefix, thresholdTime, threshold);
    }

    @Note("判断是否开启了拉黑模式")
    public boolean isEnabled() {
        return (saveKeyPrefix != null && !saveKeyPrefix.isEmpty())
                && thresholdTime != null
                && threshold > 0;
    }

    public AddBlacker saveKeyPrefix(String saveKeyPrefix) {
        this.setSaveKeyPrefix(saveKeyPrefix);
        return this;
    }

    public AddBlacker thresholdTime(long thresholdTime, Timeunit thresholdTimeunit) {
        this.setThresholdTime(thresholdTime, thresholdTimeunit);
        return this;
    }

    public AddBlacker thresholdTime(TimePair thresholdTime) {
        this.setThresholdTime(thresholdTime);
        return this;
    }

    public AddBlacker threshold(int threshold) {
        this.setThreshold(threshold);
        return this;
    }
}
